#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
#define SEED 35791246
int main(int argc, char* argv[])
{
printf("Enter the number of iterations used to estimate pi:\n");
fflush(stdout);
int niter=0;
double x,y;
int i,tid,count=0; /* # of points in the 1st quadrant of unit circle */
double z;
double pi;
scanf("%d",&niter);
#pragma omp parallel for private(x,y,z,tid) reduction(+:count)
for ( i=0; i<niter; i++) {
if (i==0)srand(SEED+omp_get_thread_num()*234);
//if(i==0) printf("the thread count is %i\n",omp_get_num_threads( ));
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = (x*x+y*y);
if (z<=1) count++;
}
printf(" the total count is %i\n",count);
pi=(((double)count)/niter)*4;
printf("# of trials= %d , estimate of pi is %g \n",niter,pi);
return 0;
}
Two Runs